﻿using BLL.Interface;
using DotNetCoreApi.Plugins.Quartz.MyJob;
using Quartz;
using System;
using System.Threading.Tasks;

namespace DotNetCoreApi.Plugins.Quartz
{
    /// <summary>
    /// 定时任务
    /// </summary>
    public class QuartzHelper
    {
        private ScheduleJobHelper scheduleJobHelper { get; set; }

        /// <summary>
        /// 循环定时器
        /// </summary>
        /// <param name="type"></param>
        /// <param name="identity"></param>
        /// <param name="delay"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public async Task RepeatForever(Type type, string identity, int delay = 1, string name = "循环定时器")
        {
            //创建一个触发器
            var trigger = TriggerBuilder.Create()
                            .StartAt(DateTime.Now.AddSeconds(delay))
                            .WithSimpleSchedule(x => x.WithIntervalInSeconds(delay).RepeatForever())//每delay秒执行一次
                            .Build();
            //创建任务
            var jobDetail = JobBuilder.Create(type)
                            .WithIdentity(identity, "group")
                            .UsingJobData("name", name)
                            .Build();
           await scheduleJobHelper.Bind(jobDetail,trigger);
        }
        /// <summary>
        /// 延时定时器
        /// </summary>
        /// <param name="type"></param>
        /// <param name="identity">定时器唯一标识</param>
        /// <param name="delay">延时</param>
        /// <param name="name"></param>
        /// <returns></returns>
        public async Task Delay(Type type, string identity, int delay = 1, string name = "延时定时器")
        {
            //创建一个触发器
            var trigger = TriggerBuilder.Create()
                            .StartAt(DateTime.Now.AddSeconds(delay))
                            .Build();

            //创建任务
            var jobDetail = JobBuilder.Create(type)
                            .WithIdentity(identity, "group")
                            .UsingJobData("name", name)
                            .Build();
            await scheduleJobHelper.Bind(jobDetail, trigger);
        }
    }
}
